package com.qian.qblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qian.qblog.common.ErrorCode;
import com.qian.qblog.common.exception.BusinessException;
import com.qian.qblog.mapper.UserMapper;
import com.qian.qblog.model.entity.User;
import com.qian.qblog.model.vo.TokenLoginUserVo;
import com.qian.qblog.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.qian.qblog.constant.SystemConstants.SALT;

/**
 * 用户服务实现
 * # @author <a href="https://github.com/lhccong">程序员聪</a>
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    public long userRegister(String userAccount, String userPassword, String checkPassword) {
        System.out.println(userAccount);
        // 1. 校验
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        if (userAccount.length() < 4) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户账号过短");
        }
        if (userPassword.length() < 8 || checkPassword.length() < 8) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户密码过短");
        }
        // 密码和校验密码相同
        if (!userPassword.equals(checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "两次输入的密码不一致");
        }

        Map<String, Object> lockMap = new ConcurrentHashMap<>();

        Object lock = lockMap.computeIfAbsent(userAccount, key -> new Object());

        synchronized (lock) {
            try {
                // 账户不能重复
                QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("userAccount", userAccount);
                long count = this.baseMapper.selectCount(queryWrapper);
                if (count > 0) {
                    throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号重复");
                }
                // 2. 加密
                String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
                // 3. 插入数据
                User user = new User();
                user.setUserAccount(userAccount);
                user.setUserPassword(encryptPassword);
                boolean saveResult = this.save(user);
                if (!saveResult) {
                    throw new BusinessException(ErrorCode.SYSTEM_ERROR, "注册失败，数据库错误");
                }
                return user.getId();
            } finally {
                // 防止内存泄漏
                lockMap.remove(userAccount);
            }
        }
    }

    public TokenLoginUserVo userLogin(String userAccount, String userPassword) {
        TokenLoginUserVo loginUserVO = new TokenLoginUserVo();
        System.out.println(loginUserVO);
        return loginUserVO;
    }

    public User getTest() {
        User user = new User();
        user.setUserAccount("admin");
        return user;
    }
}
